package com.newpage.module_lecture.activity;

import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.gyf.immersionbar.BarHide;
import com.gyf.immersionbar.ImmersionBar;
import com.newpage.module_lecture.R;
import com.wetao.lib_common.ConstantArouter;
import com.wetao.lib_common.EventBus.EventBusMessage;
import com.wetao.lib_common.EventBus.bean.TpData_command;
import com.wetao.lib_common.base.BaseActivity;
import com.wetao.lib_common.base.BasePresenter;
import com.wetao.lib_common.base.BaseViewImp;
import com.wetao.lib_common.bean.response.ExplainBean;
import com.wetao.lib_common.player.IjkMediaController;
import com.wetao.lib_common.player.IjkVideoView;
import com.wetao.lib_common.tsdBlePen.TsdService;
import com.wetao.lib_common.utils.JsonUtils;
import com.wetao.lib_common.utils.LogUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * author: zhouyh
 * created on: 2021/6/15 4:20 下午
 * description:播放mp4页面
 */
@Route(path = ConstantArouter.PATH_LECTURE_LECTUREVIDEOACTIVITY)
public class LectureVideoMpActivity extends BaseActivity implements View.OnClickListener {

    @Autowired
    String videoInfo;
    @Autowired
    String title;


    private TextView tv_title;
    private ImageView iv_exit;
    private ProgressBar pb_load_video;
    private IjkVideoView mVideoView;
    private IjkMediaController mediaController;
    private ExplainBean explainBean;


    @Override
    public int getLayoutId() {
        return R.layout.lecture_mp4_video_explain_activity;
    }

    @Override
    public BasePresenter createPresenter() {
        return null;
    }

    @Override
    public BaseViewImp createView() {
        return null;
    }

    @Override
    public void init() {
        EventBus.getDefault().register(this);

        if (videoInfo!=null){
            explainBean = JsonUtils.fromJson(videoInfo,ExplainBean.class);
        }

        initView();
        initMediaController();
        playVideo();

        EventBus.getDefault().post(new EventBusMessage(EventBusMessage.EVENT_BUS_ACTION_VIDEO_INTO, null));

    }



    private void initView(){
        mVideoView = findViewById(R.id.video_view);
        iv_exit = findViewById(R.id.iv_exit);
        tv_title = findViewById(R.id.tv_video_title);
        pb_load_video = findViewById(R.id.pb_load_video);
        tv_title.setText(title);
        iv_exit.setOnClickListener(this);
    }


    private void initMediaController(){
        mediaController = new IjkMediaController(0,1,explainBean,this);
        mediaController.setControllerListener(new IjkMediaController.ControllerListener() {
            @Override
            public void onBackClick() {
                finish();
            }

            @Override
            public void onPreClick() {

            }

            @Override
            public void onNextClick() {

            }

            @Override
            public void onFullScreenClick() {

            }

            @Override
            public void onTinyScreenClick() {

            }

            @Override
            public void onErrorViewClick() {

            }
        });
    }

    /**
     * 播放视频
     */
    private void playVideo() {
        mVideoView.setVideoPath(explainBean.getExplainUrl());
        mVideoView.setMediaController(mediaController);
        mVideoView.start();
        //设置准备完成监听
        pb_load_video.setVisibility(View.VISIBLE);
        mVideoView.setOnPreparedListener(iMediaPlayer -> {
            pb_load_video.setVisibility(View.GONE);
            mediaController.hide();
            //mVideoView.start();

        });
        mVideoView.setOnCompletionListener(iMediaPlayer -> pb_load_video.setVisibility(View.GONE));

    }

    @Override
    public boolean getIsBindTsdService() {
        return false;
    }

    @Override
    public TsdService.OnDataReceiveListener getTsdServiceDataReceiveListener() {
        return null;
    }


    @Override
    protected void onDestroy() {
        EventBus.getDefault().post(new EventBusMessage(EventBusMessage.EVENT_BUS_ACTION_VIDEO_OUT, null));
        EventBus.getDefault().unregister(this);
        super.onDestroy();
        if (mVideoView!=null){
            mVideoView.stopPlayback();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mVideoView!=null){
            mVideoView.pause();
        }
        finish();
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_exit){
            finish();
        }
    }

    @Override
    public void setImmersionBar() {
        ImmersionBar.with(this).reset();
        ImmersionBar.with(this)
                .transparentBar()
                .hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR)
                .init();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void busGetMessage(EventBusMessage messageEvent) {
        LogUtils.i("m1: bus-->" + messageEvent.toString());
        int action = messageEvent.getAction();
        String msg = messageEvent.getMsg();
        if(action == EventBusMessage.EVENT_BUS_ACTION_DATA_TP) {
            TpData_command cm = JsonUtils.fromJson(msg, TpData_command.class);
            if(cm.getId().intValue() == explainBean.getId().intValue() && cm.getUrl().equals(explainBean.getExplainUrl())) {
                //点击的是同一个格子，播放暂停
                if(mVideoView.isPlaying()) {
                    mVideoView.pause();
                } else {
                    mVideoView.start();
                }
                mediaController.hide();
            } else {
                tv_title.setText(cm.getName());
                tv_title.setText(cm.getName());
                explainBean.setId(cm.getId());
                explainBean.setExplainUrl(cm.getUrl());
                explainBean.setExplain_name(cm.getName());
                explainBean.setExplainName(cm.getName());
                mVideoView.stopPlayback();
                mVideoView.release(true);
                mVideoView.initVideoView(this);
                //mVideoView.releaseWithoutStop();
                initMediaController();
                playVideo();
            }
        }
    }

}
